function user(mongoose) {
  const userSchema = new mongoose.Schema({
    name: { type: 'string' },
    password: { type: 'string' },
    email: { type: 'string' },
    icon: { type: 'string' }, // 头像
    profile: { type: 'string' }, // 简介
    date: { type: Date, default: Date.now },
    articles: [{ type: mongoose.Schema.Types.ObjectId, ref: 'article' }],
    messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'message' }],
  });

  userSchema.statics.paging = function (find, populate, pagenumber, skip, limit) {
    if (pagenumber) {
      return this
        .findOne(find)
        .populate(populate)
        .skip(pagenumber * skip)
        .limit(limit)
        .sort({ _id: -1 });
    }
    return this
      .findOne(find)
      .populate(populate)
      .skip(pagenumber * skip)
      .limit(limit)
      .sort({ _id: -1 });
  };

  const UserModel = mongoose.model('users', userSchema);
  return UserModel;
}

function article(mongoose) {
  const articleSchema = new mongoose.Schema({
    author: { type: mongoose.Schema.Types.ObjectId, ref: 'users' }, // 待修改             
    title: { type: 'string' },
    summary: { type: 'string' },
    content: { type: 'string' }, // 内容
    messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'message' }],
    date: { type: Date, default: Date.now },
    pv: { type: 'number' },

  });

  // 静态方法，分页查询.通过_id，当然也可以用‘skip(page * 10)’方法，不过在数据量较大的时候容易有性能瓶颈,下文采用有性能瓶颈的办法，因为更简单，以后使用vue后在考虑_id的方法
  // articleSchema.statics = {
  //   paging: (id, populate) => {
  //     if (id) {
  //       return this.find({ _id: { $lt: id } })
  //         .populate(populate)
  //         .limit(10)
  //         .sort({ _id: -1 });
  //     }
  //     return this.find({})
  //       .populate(populate)
  //       .limit(10)
  //       .sort({ _id: -1 });
  //   },
  // };

  articleSchema.statics.paging = function (find, populate, pagenumber, skip, limit) {
    if (pagenumber) {
      return this
        .find(find)
        .populate(populate)
        .skip(pagenumber * skip)
        .limit(limit)
        .sort({ _id: -1 });
    }
    return this
      .find()
      .populate(populate)
      .skip(pagenumber * skip)
      .limit(limit)
      .sort({ _id: -1 });
  };

  const articleModel = mongoose.model('article', articleSchema);
  return articleModel;
}

function message(mongoose) {
  const messageSchema = new mongoose.Schema({
    mesauthor: { type: mongoose.Schema.Types.ObjectId, ref: 'users' },
    belongarticle: { type: mongoose.Schema.Types.ObjectId, ref: 'article' },
    mescontent: { type: 'string' },
    date: { type: Date, default: Date.now },
  });

  const MessageModel = mongoose.model('message', messageSchema);
  return MessageModel;
}
module.exports = {
  user,
  article,
  message,
};
